//
//  ChooseTopPersonViewController.m
//  HuaRun2
//
//  Created by admin on 2017/11/2.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "ChooseTopPersonViewController.h"
#import "ContactPersonCell.h"
#import "Contact_ListModel.h"

@implementation ChooseTopPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = NSLocalizedString(@"contacts_list", nil);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ContactPersonCell class] forCellReuseIdentifier:@"ContactPersonCell"];
    self.searchView.placeholderStr = NSLocalizedString(@"搜索联系人名称", nil);

}
- (void)dataInit {
    [super dataInit];
    
    [self searchMethod];

}

-(void)methodBind {
    
    weakself(ws);
    [self.tableView headerRefresh:^{
        [ws refreshData];
    }];
    [self.searchView.textField addTarget:self action:@selector(recoverOriginDataMethod:)
                        forControlEvents:(UIControlEventEditingChanged)];
    
    self.searchView.clickBlock = ^(id result) {
        [ws searchMethod];
    };
    
    self.normalDZNEmptyDataSouce.reloadBlock = ^{
        [ws dataInit];
    };
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactPersonCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ContactPersonCell" forIndexPath:indexPath];
    cell.personModel = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Contact_ListModel * personModel = self.dataArray[indexPath.row];
    self.clickBlock ? self.clickBlock(personModel) : nil;
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - method
- (void)recoverOriginDataMethod:(UITextField *)textField {
    if (textField.text.length == 0) {
        NSLog(@"恢复最初数据");
        [self searchMethod];
    }else {
        NSLog(@"准备搜索");
    }
}

//    联系人参数：orderBy，取值：name create modify，默认为name
- (void)searchMethod {
    [self.view endEditing:YES];
    
    NSString * keyword = [NSString isemptyStr:self.searchView.textField.text] ? @"" : self.searchView.textField.text;
    
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kContactListUrl];
    
    NSDictionary * dic = @{@"current": @(self.current),
                           @"size": @(self.size),
                           @"keyword": keyword,
                           @"salutation": @"",
                           @"category": @"",
                           @"orderBy": @"name"
                           };
    weakself(ws);
    
    [self showActivity:kLodingStr];
    
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        [ws hideAllAlert];
        [ws.tableView endHeaderRefresh];
        
        if (![object[@"code"] isEqual: kSuccessCode]) {
            [ws showAlert:object[@"msg"] ? object[@"msg"] : @"" ];
            
            return;
        }
        NSArray * array = [NSArray yy_modelArrayWithClass:[Contact_ListModel class] json:object[@"list"]];
        
        ws.dataArray = ws.current == 1 ? array : [ws.dataArray arrayByAddingObjectsFromArray:array];
        [ws.tableView reloadData];
        
        if (array.count >= ws.size) {
            
            [ws.tableView footerRefresh:^{
                [ws loadMoreData];
            }];
        }else {
            [ws.tableView hiddenFooter];
        }
        
    } failure:^(NSError *error) {
        [ws hideAllAlert];
        
        //        NSLog(@"fail %@", error);
        [ws.tableView endHeaderRefresh];
        [ws showWithText:kNetworkError];
        [ws.normalDZNEmptyDataSouce setDznEmptyStatus:(DZNEmptyStatusFailure)];
        
    }];
    
}
- (void)refreshData {
    self.current = 1;
    self.searchView.textField.text = @"";
    
    [self searchMethod];
}

- (void)loadMoreData {
    self.current++;
    [self searchMethod];
}
@end
